<template>
	<div>
		<el-form ref="form" :model="params1.wasteInfo" :rules="params1.rules" label-width="80px">
			<el-form-item label="固废代码" prop="wasteCode">
				<el-input v-model="params1.wasteInfo.wasteCode" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="固废名称" prop="wasteName">
				<el-input v-model="params1.wasteInfo.wasteName" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="计量单位" prop="unitCode">
				<el-select v-model="params1.wasteInfo.unitCode" style="width: 100%;">
					<el-option :label="item.value" :value="item.code" v-for="(item,index) in params1.unitList" :key="index">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="固废分类" prop="wasteId">
				<el-select v-model="params1.wasteInfo.wasteId" style="width: 100%;" @change="changeWasteId">
					<el-option :label="item.wasteName" :value="item.id" v-for="(item,index) in params1.wasteIdList" :key="item.id">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="固废类型" prop="wasteClassification">
				<el-select v-model="params1.wasteInfo.wasteClassification" style="width: 100%;">
					<el-option :label="item.label" :value="item.value" v-for="(item,index) in params1.wasteTypeList" :key="index">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="物理特性" prop="physicalProperty">
				<el-select v-model="params1.wasteInfo.physicalProperty" style="width: 100%;">
					<el-option :label="item.label" :value="item.value" v-for="(item,index) in params1.typeList" :key="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="产污环节" prop="productionLink">
				<el-input v-model="params1.wasteInfo.productionLink" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="主要成分" prop="mainFeatures">
				<el-input v-model="params1.wasteInfo.mainFeatures" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="污染特征" prop="pollutionCharacteristics">
				<el-input v-model="params1.wasteInfo.pollutionCharacteristics" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item style="text-align: center;">
				<el-button v-if="editData && editData.id" type="danger" @click="deleteThis">删除</el-button>
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	let that
	export default {
		name: 'addSwWaste',
		components: {},
		props: ['entId', 'editData'],
		data() {
			return {
				params: {},
				params1: {
					visible: false,
					wasteInfo: {
						dangerTypeList: [],
					},
					unitList: [],
					typeList: [{
							label: '固态',
							value: 'SOLID'
						},
						{
							label: '半固态',
							value: 'SEMISOLID'
						},
						{
							label: '液态',
							value: 'LIQUID'
						},
						{
							label: '其他形态',
							value: 'OTHER_PATTERN'
						},
					],
					enterpriseId: '',
					rules: {
						wasteCode: [{
							required: true,
							message: '请输入危废代码',
							trigger: 'blur'
						}],
						wasteName: [{
							required: true,
							message: '请输入危废名称',
							trigger: 'blur'
						}],
						unitCode: [{
							required: true,
							message: '请选择计量单位',
							trigger: 'blur'
						}],
						wasteId: [{
							required: true,
							message: '请选择废物分类',
							trigger: 'blur'
						}],
						wasteClassification: [{
							required: true,
							message: '请选择废物类型',
							trigger: 'blur'
						}],
						physicalProperty: [{
							required: true,
							message: '物理特性',
							trigger: 'blur'
						}],
						productionLink: [{
							required: true,
							message: '产污环节',
							trigger: 'blur'
						}],
						mainFeatures: [{
							required: true,
							message: '主要成分',
							trigger: 'blur'
						}],
						pollutionCharacteristics: [{
							required: true,
							message: '污染特征',
							trigger: 'blur'
						}],
					},
					wasteTypeList: [{
							label: '第1类一般工业固体废物',
							value: 'ONE_CLASS_WASTE'
						},
						{
							label: '第2类一般工业固体废物',
							value: 'TWO_CLASS_WASTE'
						}
					],
					wasteIdList: [],
				},
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.itemId = that.getQueryString("itemId")
			that.params.ticketId = that.$store.state.token

			that.getUnitList();
			that.getSwWasteClassificationList();

			if (that.editData) {
				that.params1.wasteInfo = that.editData
				that.params1.wasteInfo.dangerTypeList = that.params1.wasteInfo.dangerTypeList || [];
			}
		},
		methods: {
			cancle() {
				that.$emit("cancle", true)
			},

			changeWasteId(value) {
				let thisIndex = that.params1.wasteIdList.findIndex(item => item.id == value)
				that.params1.wasteInfo.wasteId = that.params1.wasteIdList[thisIndex].id
				that.params1.wasteInfo.wasteCode = that.params1.wasteIdList[thisIndex].wasteCode
			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/sw/entWaste/deleteSwEntWaste?ticketId=' + that.params.ticketId,
						data: {
							entWasteId: that.editData.id
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.status && result.data) {
								that.$emit("success", true)
							}
						}
					})
				})
			},

			getSwWasteClassificationList: function() {
				var _this = this;
				that.ajax({
					url: '/sw/wasteClassification/getSwWasteClassificationList',
					data: {},
					success: function(result) {
						if (result.status == 1) {
							that.params1.wasteIdList = result.data
						}
					}
				});
			},

			getUnitList: function() {
				var _this = this;
				that.ajax({
					url: '/codeType/listCodeValue',
					data: {
						typeCode: 'UNIT_TYPE'
					},
					success: function(data) {
						if (data.status == 1) {
							_this.params1.unitList = data.data;
						}
					}
				});
			},

			saveOrUpdate: function() {
				var _this = this;
				_this.$refs['form'].validate(function(valid) {
					if (valid) {
						//that.params1.wasteInfo.facilitatorId = that.params.facilitatorId
						that.ajax({
							url: '/sw/entWaste/saveOrUpdateSwEntWaste?ticketId=' + that.params
								.ticketId,
							data: JSON.stringify(that.params1.wasteInfo),
							contentType: 'application/json',
							success: function(data) {
								console.log(data)
								if (data.status == 1) {
									//that.data.params1 = that.$options.data().params1
									if (!that.params1.wasteInfo.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
								}
							}
						});
					}
				})
			},

			querySearchName: function(queryString, cb) {
				that.ajax({
					url: '/entWaste/listWasteName',
					data: {
						keyword: queryString,
						wasteid: this.params1.wasteInfo.wasteId
					},
					success: function(result) {
						if (result.status == 1 && result.data) {
							cb(result.data);
						}
					}
				});
			},

			handleSelectName: function(item) {
				var _this = this;
				var wasteInfo = _this.params1.wasteInfo;
				wasteInfo.wasteName = item.name;
				wasteInfo.wasteNameId = item.id;
				_this.params1.wasteInfo = wasteInfo;
			},

			handleSelect: function(item) {
				var _this = this;
				var wasteInfo = _this.params1.wasteInfo;
				wasteInfo.wasteCode = item.addenterwastecode;
				wasteInfo.wasteId = item.id;
				_this.params1.wasteInfo = wasteInfo;
			},

			querySearch: function(queryString, cb) {
				var _this = this;
				that.ajax({
					url: '/entWaste/getWasteIdDropDownList.htm',
					data: {
						keyword: queryString
					},
					success: function(result) {
						for (var i in result.value) {
							result.value[i]['addenterwastecode'] = result.value[i][
									'addenterwastecode'
								]
								.replace(/<font color='red'>/g, '').replace(/<\/font>/g, '');
							result.value[i]['addenterwastename'] = result.value[i][
									'addenterwastename'
								]
								.replace(/<font color='red'>/g, '').replace(/<\/font>/g, '');
							if (queryString == result.value[i]['addenterwastecode']) {
								var wasteInfo = _this.wasteInfo;
								wasteInfo.wasteId = result.value[i].id;
								_this.wasteInfo = wasteInfo;
							}
						}
						cb(result.value);
					}
				});
			},
		}
	}
</script>

<style>

</style>